//
//  BaseNavigationController.m
//  Rockval
//
//  Created by TuanNT on 4/27/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "BaseNavigationController.h"
#import "RVNavigationTitleView.h"
#import "CRGradientNavigationBar.h"
#import "SSToolkit.h"
#import <objc/runtime.h>

static char UIViewControllerTitleKey;
static char UIViewControllerSubtitleKey;

@implementation UIViewController (RVNavigationSubTitle)
@dynamic title;
@dynamic subtitle;

- (NSString *)title {
	return objc_getAssociatedObject(self, &UIViewControllerTitleKey);
}

- (void)setTitle:(NSString *)title {
	[self willChangeValueForKey:@"title"];
	objc_setAssociatedObject(self, &UIViewControllerTitleKey, title, OBJC_ASSOCIATION_COPY_NONATOMIC);
	[self didChangeValueForKey:@"title"];
	[self updateTitleTo:title];
	self.navigationItem.title = title;
}

- (NSString *)subtitle {
	return objc_getAssociatedObject(self, &UIViewControllerSubtitleKey);
}

- (void)setSubtitle:(NSString *)subtitle {
	[self willChangeValueForKey:@"subtitle"];
	objc_setAssociatedObject(self, &UIViewControllerSubtitleKey, subtitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
	[self didChangeValueForKey:@"subtitle"];
	[self updateSubtitleTo:subtitle];
}

- (void)updateTitleTo:(NSString *)title {
	if ([self.navigationItem.titleView isKindOfClass:[RVNavigationTitleView class]]) {
		[(RVNavigationTitleView *)self.navigationItem.titleView setNavigationBarTitle : title];
	}
}

- (void)updateSubtitleTo:(NSString *)subtitle {
	if ([self.navigationItem.titleView isKindOfClass:[RVNavigationTitleView class]]) {
		[(RVNavigationTitleView *)self.navigationItem.titleView setNavigationBarSubtitle : subtitle];
	}
}

@end

@implementation BaseNavigationController

- (id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder])) return nil;

	CRGradientNavigationBar *navBar = [[CRGradientNavigationBar alloc] init];
	[self setValue:navBar forKey:@"navigationBar"];

	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self configNavigationBar];
	[self addTitleViewToViewController:self.topViewController];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (void)setViewControllers:(NSArray *)viewControllers {
	[self addTitleViewToViewController:[viewControllers lastObject]];
	[super setViewControllers:viewControllers];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
	[self addTitleViewToViewController:viewController];
	[super pushViewController:viewController animated:animated];
}

- (void)addTitleViewToViewController:(UIViewController *)viewController {
	if (!viewController.navigationItem.titleView) {
		RVNavigationTitleView *titleView = [[RVNavigationTitleView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
		viewController.navigationItem.titleView = titleView;
		viewController.title = viewController.title.length ? viewController.title : viewController.navigationItem.title;
	}
}

- (void)configNavigationBar {
	CRGradientNavigationBar *navigationBar = (CRGradientNavigationBar *)self.navigationBar;
	if ([navigationBar respondsToSelector:@selector(barTintColor)]) {
		[navigationBar performSelector:@selector(setBarTintColor:) withObject:[UIColor colorWithRed:0.350 green:0.403 blue:0.521 alpha:1.000]];
	}
	[navigationBar setTranslucent:NO];

	UIColor *firstColor = [UIColor colorWithRed:0.498 green:0.549 blue:0.675 alpha:1.000];
	UIColor *secondColor = [UIColor colorWithRed:0.235 green:0.282 blue:0.384 alpha:1.000];

	NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
	// or NSArray *colors = [NSArray arrayWithObjects:(id)UIColorFromRGB(0xf16149).CGColor, (id)UIColorFromRGB(0xf14959).CGColor, nil];

	[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];

	SAMGradientView *gradientView = [[SAMGradientView alloc] initWithFrame:self.toolbar.bounds];
	// Set the gradient colors
	gradientView.gradientColors = @[[UIColor colorWithRed:0.498 green:0.549 blue:0.675 alpha:1.000],
	                                [UIColor colorWithRed:0.235 green:0.282 blue:0.384 alpha:1.000]];

	// Optionally change the direction. The default is vertical.
	gradientView.gradientDirection = SAMGradientViewDirectionVertical;

	// Add it as a subview in all of its awesome
	[self.toolbar addSubview:gradientView];
	[self.toolbar sendSubviewToBack:gradientView];
}

@end
